package com.aplikata.evok.ui;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

import org.hibernate.criterion.Order;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.TreeNode;

import com.aplikata.bo.Role;
import com.aplikata.dataModel.RoleDataModel;
import com.aplikata.evok.service.EvokQtnaireService;
import com.aplikata.ui.UIBean;
import com.aplikata.util.FacesUtil;

/**
 * @ClassName: RoleBean
 * @Description: TODO
 * @author Huyun
 * @date 27. 2. 2012. 08:47:14
 */
@ManagedBean(name = "evokQtnaireRoleBean")
@RequestScoped
public class EvokQtnaireRoleBean extends UIBean {
	private static final long serialVersionUID = 1L;

	@ManagedProperty(value = "#{evokQtnaireService}")
	private EvokQtnaireService evokQtnaireService;

	private RoleDataModel roleModel;
	private List<Role> roleList;

	private Role role;

	private TreeNode rootQtnaire;
	private TreeNode[] selectedQtnaires;

	public EvokQtnaireRoleBean() {

	}

	public void submit() {
		try {
			evokQtnaireService.updateRoleQtnaires(role, selectedQtnaires);
			FacesUtil.addInfoMessage("info_content_edit_success", new Object[] { role.getName() });
			role = null;
			rootQtnaire = null;
		} catch (Exception e) {
			FacesUtil.addActionException(e, log);
		}
	}

	public void onRowSelect(SelectEvent event) {
		role = (Role) event.getObject();
		rootQtnaire = null;
		rootQtnaire = this.getRootQtnaire();
	}

	@SuppressWarnings("unchecked")
	public List<Role> getRoleList() {
		if (roleList == null) {
			roleList = (List<Role>) evokQtnaireService.findAll(Role.class, new Order[] { Order.asc(Role.NAME) });
		}

		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public RoleDataModel getRoleModel() {
		if (roleModel == null) {
			roleModel = new RoleDataModel(evokQtnaireService, getRoleList());
		}
		return roleModel;
	}

	public void setRoleModel(RoleDataModel roleModel) {
		this.roleModel = roleModel;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public EvokQtnaireService getEvokQtnaireService() {
		return evokQtnaireService;
	}

	public void setEvokQtnaireService(EvokQtnaireService evokQtnaireService) {
		this.evokQtnaireService = evokQtnaireService;
	}

	public TreeNode getRootQtnaire() {
		if (rootQtnaire == null) {
			rootQtnaire = evokQtnaireService.getRootQtnaire(role);
		}
		return rootQtnaire;
	}

	public void setRootQtnaire(TreeNode rootQtnaire) {
		this.rootQtnaire = rootQtnaire;
	}

	public TreeNode[] getSelectedQtnaires() {
		return selectedQtnaires;
	}

	public void setSelectedQtnaires(TreeNode[] selectedQtnaires) {
		this.selectedQtnaires = selectedQtnaires;
	}

	public SelectItem[] getRoleItems() {
		roleList = this.getRoleList();
		
		if (roleList == null || roleList.isEmpty())
			return null;

		SelectItem[] items = new SelectItem[roleList.size()+1];
		items[0] = new SelectItem("","");
		for (int i=0; i<roleList.size(); i++) {
			Role role = roleList.get(i);
			items[i+1] = new SelectItem(role.getId().toString(), role.getName());
		}
		return items;
	}
	
	
}
